1835C - Twin Clusters - CodeForces Solution


bitmasks math meet-in-the-middle probabilities

Please click on ads to support us..

C++ Code:

#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <deque>
#include <ctime>
#include <unordered_map>
#include <math.h>
#include <bitset>
#include <iomanip>
#include <string>
using namespace std;
#define int long long
#define mod99 998244353
#define f first
#define s second
#define pb push_back
#define mod 998244353
#define INF 1e9 + 7
#define ll long long
#define get_time (double)clock() / (double)(CLOCKS_PER_SEC)
#ifndef ONLINE_JUDGE
#define debug(x) cerr << (#x) << ": " << x << endl;
#else
#define debug(x)
#endif
//#define endl '\n'
int min(int a, int b) {
    if (a <= b) return a;
    return b;
}
int max(int a, int b) {
    if (a >= b) return a;
    return b;
}
int gcd(int a, int b) {
    while (a > 0 && b > 0) {
        int c = a;
        a = b % a;
        b = c;
    }
    return a + b;
}
int bin_pow(int a, int n) {
    if (n == 0) return 1;
    if (n % 2 == 0) {
        ll t = bin_pow(a, n / 2) % mod;
        return (t * t) % mod;
    }
    else {
        ll t = bin_pow(a, n - 1);
        return (t * a) % mod;
    }
}
int bs_sqrt(int x) {
    int l = 0, r = INF;
    while (r - l > 1) {
        int m = (r + l) / 2;
        if (m * m <= x) {
            l = m;
        }
        else {
            r = m;
        }
    }
    return l;
}
vector<int> in_2(int a) {
    vector<int> ret;
    while (a > 0) {
        ret.push_back(a % 2);
        a /= 2;
    }
    return ret;
}
int rev(int a) {
    return bin_pow(a, mod - 2);
}
void solve() {
    int k;
    cin >> k;
    int n = bin_pow(2, k + 1);
    vector<int> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    vector<pair<int, int> > otr;
    vector<int> pref(n + 1);
    for (int i = 1; i <= n; i++) pref[i] = pref[i - 1] ^ a[i - 1];
    map<int, int> last;
    last[0] = -1;
    int cur = 0;
    for (int i = 0; i < n; i++) {
        cur = (cur ^ a[i]) % bin_pow(2, k);
        if (last.count(cur)) {
            otr.push_back({ last[cur] + 1, i });
        }
        last[cur] = i;
    }
    map<int, pair<int, int> > get;
    for (int i = 0; i < otr.size(); i++) {
        int l = otr[i].first, r = otr[i].second;
        int cur_xor = pref[r + 1] ^ pref[l];
        if (get.count(cur_xor)) {
            int lp = get[cur_xor].first, rp = get[cur_xor].second;
            if (lp < l) {
                swap(l, lp);
                swap(r, rp);
            }
            if (r < lp) {
                cout << l + 1 << " " << r + 1 << " " << lp + 1 << " " << rp + 1 << endl;
                return;
            }
            cout << l + 1 << " " << lp - 1 + 1 << " " << min(rp, r) + 1 + 1  << " " << max(rp, r) + 1 << endl;
            return;
        }
        get[cur_xor] = { l,r };
    }

}
signed main() {
    cout << fixed << setprecision(20);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
}
/*
1
4 6 5 2
1 2 1
2 3 1
3 4 1
1 2 70
2 3 120
3 4 4
4 90
4 2
4 10
4 37
2 34
*/


Comments

Submit
0 Comments
More Questions

780C - Andryusha and Colored Balloons
1153A - Serval and Bus
1487C - Minimum Ties
1136A - Nastya Is Reading a Book
1353B - Two Arrays And Swaps
1490E - Accidental Victory
1335A - Candies and Two Sisters
96B - Lucky Numbers (easy)
1151B - Dima and a Bad XOR
1435B - A New Technique
1633A - Div 7
268A - Games
1062B - Math
1294C - Product of Three Numbers
749A - Bachgold Problem
1486B - Eastern Exhibition
1363A - Odd Selection
131B - Opposites Attract
490C - Hacking Cypher
158B - Taxi
41C - Email address
1373D - Maximum Sum on Even Positions
1574C - Slay the Dragon
621A - Wet Shark and Odd and Even
1395A - Boboniu Likes to Color Balls
1637C - Andrew and Stones
1334B - Middle Class
260C - Balls and Boxes
1554A - Cherry
11B - Jumping Jack